'use strict';

angular.module('afasApp')
  .controller('TimeClassesCtrl', ['$scope', 'Services', '$state', '$filter', function ($scope, Services, $state, $filter) {

    console.log($filter);
    // timeTemp.setHours ( timeTemp.getHours() + 8 );

    $scope.timeClasses = [];
    $scope.timeClassNew = {
      'StartTime': '',
      'EndTime': ''
    };
    $scope.coursetypes = [];

    // $scope.dayClasses = DayClasses.getDayClasses();
    Services.getItems('course_types')
    .then(function(results){
      $scope.coursetypes = results.data;
    });

    function applyRemoteData( newData ) {
      $scope.timeClasses = newData;
    }

    function loadRemoteData() {
      Services.getItems($scope.TIMECLASS_URL, {'include': 'course_type'})
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    $scope.addTimeClass = function () {

      $scope.timeClassNew.StartTime = $filter('date')($scope.timepicker.timeA, 'HH:mm:ss');
      $scope.timeClassNew.EndTime = $filter('date')($scope.timepicker.timeB, 'HH:mm:ss');

      Services.add($scope.TIMECLASS_URL, $scope.timeClassNew)
      .then(function(){
        $scope.timeClassNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.deleteTimeClass = function (timeClass) {
      Services.delete($scope.TIMECLASS_URL, timeClass)
      .then(function(){
        loadRemoteData();
      });
    };

  }]).controller('TimeClassCtrl', ['$scope', 'Services', '$state', '$stateParams', '$filter', function ($scope, Services, $state, $stateParams, $filter) {

    console.log($filter);

    $scope.currentTimeClass = {};
    $scope.currentID = $stateParams.id;
    $scope.currentTimeClass = {};
    $scope.coursetypes = [];


    // $scope.dayClasses = DayClasses.getDayClasses();
    Services.getItems('course_types')
    .then(function(results){
      $scope.coursetypes = results.data;
    });

    Services.getItem($scope.TIMECLASS_URL, $stateParams.id, {'include': 'course_type'})
    .then(function(results){
      $scope.currentTimeClass = results.data;
      $scope.currentTimeClass.StartTime = $filter('date')($scope.currentTimeClass.StartTime, 'yyyy-MM-dd HH:mm:ss');
      $scope.currentTimeClass.EndTime = $filter('date')($scope.currentTimeClass.EndTime, 'yyyy-MM-dd HH:mm:ss');
    });

    $scope.updateTimeClass = function () {
      $scope.currentTimeClass.StartTime = $filter('date')($scope.currentTimeClass.StartTime, 'HH:mm:ss');
      $scope.currentTimeClass.EndTime = $filter('date')($scope.currentTimeClass.EndTime, 'HH:mm:ss');

      Services.update($scope.TIMECLASS_URL, $scope.currentTimeClass, $scope.currentID)
      .then(function(){
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };

  }]);